package database;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeansException;

import database.dao.SingleSongScoreInfoDao;
import database.pojo.SingleSongScoreInfo;
import manager.player.basic.type.songRankInfo;
import manager.rank.type.singSongNumInfo;
import util.JdbcUtils;
import util.MyFactory;

public class SingleSongScoreDBServer {
	public SingleSongScoreInfo insertSingleSongScoreInfo(SingleSongScoreInfo pInfo) throws SQLException{
		Connection conn = null;
		try{
			conn = MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).getConnection();
			MyFactory.context.getBean("SingleSongScoreInfoDao", SingleSongScoreInfoDao.class).insert(pInfo, conn);
		}finally{
			MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).releaseConnection(conn);
		}
		return pInfo;
	}
	
	public SingleSongScoreInfo updateSingleSongScoreInfo(SingleSongScoreInfo pInfo) throws SQLException{
		Connection conn = null;
		try{
			conn = MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).getConnection();
			MyFactory.context.getBean("SingleSongScoreInfoDao", SingleSongScoreInfoDao.class).update(pInfo, conn);
		}finally{
			MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).releaseConnection(conn);
		}
		return pInfo;
	}

	public void updateRank(String openId,String songId,int rank) throws BeansException, SQLException {
		
		Connection conn = null;
		try{
			conn = MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).getConnection();
			MyFactory.context.getBean("SingleSongScoreInfoDao", SingleSongScoreInfoDao.class).updateRank(openId, songId, rank, conn);
		}finally{
			MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).releaseConnection(conn);
		}
	}
	
	public int  selectRank(String songId,int score) throws BeansException, SQLException {
		Connection conn = null;
		try{
			conn = MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).getConnection();
			return MyFactory.context.getBean("SingleSongScoreInfoDao", SingleSongScoreInfoDao.class).selectRank( songId, score, conn);
		}finally{
			MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).releaseConnection(conn);
		}
	}
	
	public boolean isExistSongId(String songId) throws BeansException, SQLException {
		Connection conn = null;
		try{
			conn = MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).getConnection();
			return MyFactory.context.getBean("SingleSongScoreInfoDao", SingleSongScoreInfoDao.class).isExistSongId(songId, conn);
		}finally{
			MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).releaseConnection(conn);
		}
	}
	public ArrayList<Integer> selectScoreByOpenIdAndSongId(String openId,String songId) throws BeansException, SQLException {
		
		Connection conn = null;
		try{
			conn = MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).getConnection();
			return MyFactory.context.getBean("SingleSongScoreInfoDao", SingleSongScoreInfoDao.class).selectScoreByOpenIdAndSongId(openId, songId, conn);
		}finally{
			MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).releaseConnection(conn);
		}
	}
	
public ArrayList<SingleSongScoreInfo> select(String songId,int number,int rankRange) throws BeansException, SQLException {
		
		Connection conn = null;
		try{
			conn = MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).getConnection();
			
			return MyFactory.context.getBean("SingleSongScoreInfoDao", SingleSongScoreInfoDao.class).select(songId, number, conn);
			
		}finally{
			MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).releaseConnection(conn);
		}
	}

public int selectMaxScore(String openId, String songId) throws BeansException, SQLException {
	Connection conn = null;
	try{
		conn = MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).getConnection();
		return MyFactory.context.getBean("SingleSongScoreInfoDao", SingleSongScoreInfoDao.class).selectMaxScore(openId, songId, conn);
	}finally{
		MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).releaseConnection(conn);
	}
}

public ArrayList<SingleSongScoreInfo> getOrderInfo(String songId, int number, int rankRange) throws BeansException, SQLException {
	Connection conn = null;
	try{
		conn = MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).getConnection();
		if(rankRange == 0) {
			return MyFactory.context.getBean("SingleSongScoreInfoDao", SingleSongScoreInfoDao.class).getAllOrderInfo(songId, number, conn);
		}else {
			return MyFactory.context.getBean("SingleSongScoreInfoDao", SingleSongScoreInfoDao.class).getOrderInfoByMid(songId, number,rankRange, conn);
		}
	}finally{
		MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).releaseConnection(conn);
	}
}

public ArrayList<singSongNumInfo> getsingSongNumList(int number, int rankRange) throws BeansException, SQLException {
	Connection conn = null;
	try{
		conn = MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).getConnection();
		if(rankRange == 0) {
			return MyFactory.context.getBean("SingleSongScoreInfoDao", SingleSongScoreInfoDao.class).getAllOrderNum( number, conn);
		}else {
			return MyFactory.context.getBean("SingleSongScoreInfoDao", SingleSongScoreInfoDao.class).getOrderNumByMid(number,rankRange, conn);
		}
	}finally{
		MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).releaseConnection(conn);
	}
}

public int getTotalNumOfSongId(String songId, int rankRange) throws BeansException, SQLException {
	Connection conn = null;
	try{
		conn = MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).getConnection();
		if(rankRange == 0) {
			return MyFactory.context.getBean("SingleSongScoreInfoDao", SingleSongScoreInfoDao.class).getAllTotalNumOfSongId(songId,conn);
		}else {
			return MyFactory.context.getBean("SingleSongScoreInfoDao", SingleSongScoreInfoDao.class).getTotalNumOfSongId(songId,rankRange, conn);
		}
	}finally{
		MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).releaseConnection(conn);
	}
}

public int selectNumByOpenId(String openId, int rankRange) throws BeansException, SQLException {
	Connection conn = null;
	try{
		conn = MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).getConnection();
		if(rankRange == 0) {
			return MyFactory.context.getBean("SingleSongScoreInfoDao", SingleSongScoreInfoDao.class).selectAllNumByOpenId(openId,conn);
		}else {
			return MyFactory.context.getBean("SingleSongScoreInfoDao", SingleSongScoreInfoDao.class).selectNumByOpenId(openId,rankRange, conn);
		}
	}finally{
		MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).releaseConnection(conn);
	}
}



}